<?php

class Kd_Util_String {

    static public function lowerFirstChar($str) {
        return strtolower(substr($str, 0, 1)) . substr($str, 1);
    }

    static public function upperFirstChar($str) {
        return ucfirst($str);
    }

    static public function underlineToWord($str) {
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
    }

    static public function wordToUnderline($str) {
        return strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $str));
    }

    static public function format($str, $values) {
        $result = $str;
        if (preg_match_all('/(\{\w+\})/i', $str, $matches)) {
            foreach ($matches[0] as $match) {
                $key = substr($match, 1, -1);
                if (array_key_exists($key, $values)) {
                    $result = str_replace($match, $values[$key], $result);
                }
            }
        }
        return $result;
    }

    static public function startsWith($str, $search) {
        $len = strlen($search);
        return substr($str, 0, $len) == $search;
    }

    static public function endsWith($str, $search) {
        $searchLen = strlen($search);
        $strLen = strlen($str);
        return substr($str, $strLen - $searchLen) == $search;
    }

    static public function subString($str, $flag, $before = TRUE){
    	if($before){
    		$pos = strpos($str, $flag);
    		if($pos === FALSE){
    			return $str;
    		}
    		return substr($str, 0, $pos);
    	}
    	return strstr($str, $flag);
    }
}